<template>
  <div class="bg-bg_color" style="padding: 20px">
    <!-- 搜索框部分 -->
    <div>
      <el-form :inline="true" :model="findCondition" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="findCondition.userName" placeholder="请输入姓名" clearable>
            <template #suffix>
              <IconifyIconOnline icon="material-symbols:search" class="el-input__icon" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="findCondition.phoneNub" placeholder="请输入电话" clearable>
            <template #suffix>
              <IconifyIconOnline icon="material-symbols:phone-android-outline" class="el-input__icon" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="findCondition.address" placeholder="请输入地址" clearable>
            <template #suffix>
              <IconifyIconOnline icon="material-symbols:edit-location-alt-outline" class="el-input__icon" />
            </template>
          </el-input>
        </el-form-item>

        <!-- 按键操作 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit"> <IconifyIconOnline icon="material-symbols:search" class="el-icon--left" />查询 </el-button>
          <el-button type="" @click="onReset"><IconifyIconOnline icon="material-symbols:wifi-protected-setup" class="el-icon--left" />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="flex justify-between" style="margin-bottom: 20px">
      <h3 />
      <!-- <el-button type="primary" @click="addFormVisible = true">
        <IconifyIconOnline icon="material-symbols:add-circle-outline" class="el-icon--left" />今日打卡</el-button
      > -->
    </div>

    <!-- 表格部分 -->
    <div>
      <el-table :data="recordListData.list" stripe border>
        <el-table-column align="center" type="index" width="50" />
        <el-table-column align="center" prop="userName" label="姓名" width="100" />
        <el-table-column align="center" prop="sex" label="姓别" width="60">
          <template #default="scope">
            <el-tag v-if="scope.row.sex">男</el-tag>
            <el-tag v-else type="warning">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="phoneNub" label="电话" width="120" />
        <el-table-column align="center" prop="bodyState" label="身体状态" min-width="150">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.bodyState == '无任何症状,健康'">无任何症状, 健康</el-tag>
            <!-- <el-tag type="danger" v-else-if="scope.row.yinOrYang == '阳性'">阳性</el-tag> -->
            <el-tag type="danger" v-else>{{ scope.row.bodyState }}</el-tag>
          </template>
          <!-- <el-tag>男</el-tag> -->
        </el-table-column>
        <el-table-column align="center" prop="temperature" label="体温" width="80" />
        <el-table-column align="center" prop="yinOrYang" label="阴阳" width="90">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.yinOrYang == '阴性'">阴性</el-tag>
            <el-tag type="danger" v-else-if="scope.row.yinOrYang == '阳性'">阳性</el-tag>
            <el-tag type="info" v-else>待检测</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="recordAddress" label="打卡地点" min-width="150" />
        <el-table-column align="center" prop="createTime" label="打卡时间" width="180" />
        <el-table-column align="center" fixed="right" prop="name" label="操作" width="220">
          <template #default="scope">
            <el-button size="small" type="success" @click="editUser(scope.row)"
              ><IconifyIconOnline icon="material-symbols:edit-square-outline" class="el-icon--left" />编辑</el-button
            >
            <el-button size="small" type="danger" @click="deleteRecord(scope.row.id)"
              ><IconifyIconOnline icon="material-symbols:delete-forever-outline-rounded" class="el-icon--left" />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页部分 -->
    <div class="flex justify-end" style="margin-top: 20px">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSet.pageSize"
        v-model:current-page="pageSet.pageNub"
        layout="prev, pager, next"
        :total="recordListData.totle"
      />
    </div>

    <!-- 添加 -->
    <el-dialog v-model="addFormVisible" title="添加用户">
      <el-form ref="ruleFormRef" label-position="right" label-width="100px" :model="addRecordCardData" style="max-width: 360px">
        <el-form-item label="身体状态">
          <el-select v-model="addRecordCardData.bodyState">
            <el-option label="无任何症状,健康" value="无任何症状,健康" />
            <el-option label="发热" value="发热" />
            <el-option label="喉咙痛" value="喉咙痛" />
            <el-option label="发热,喉咙痛" value="发热,喉咙痛" />
          </el-select>
        </el-form-item>
        <el-form-item label="体温">
          <el-input v-model="addRecordCardData.temperature" />
        </el-form-item>
        <el-form-item label="阴阳">
          <el-radio-group v-model="addRecordCardData.yinOrYang">
            <el-radio label="阴性" size="large">阴性</el-radio>
            <el-radio label="阳性" size="large">阳性</el-radio>
            <el-radio label="待检测" size="large">待检测</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="打卡地点">
          <el-input v-model="addRecordCardData.recordAddress" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAddForm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog v-model="editFormVisible" title="编辑">
      <el-form ref="ruleFormRef" label-position="right" label-width="100px" :model="updateRecordCardData" style="max-width: 360px">
        <el-form-item label="身体状态">
          <el-select v-model="updateRecordCardData.bodyState">
            <el-option label="无任何症状,健康" value="无任何症状,健康" />
            <el-option label="发热" value="发热" />
            <el-option label="喉咙痛" value="喉咙痛" />
            <el-option label="发热,喉咙痛" value="发热,喉咙痛" />
          </el-select>
        </el-form-item>
        <el-form-item label="体温">
          <el-input v-model="updateRecordCardData.temperature" />
        </el-form-item>
        <el-form-item label="阴阳">
          <el-radio-group v-model="updateRecordCardData.yinOrYang">
            <el-radio label="阴性" size="large">阴性</el-radio>
            <el-radio label="阳性" size="large">阳性</el-radio>
            <el-radio label="待检测" size="large">待检测</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="打卡地点">
          <el-input v-model="updateRecordCardData.recordAddress" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitEditForm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { message } from "@/utils/message";
import { ref, reactive, onMounted } from "vue";
import { deleteRecordById, recordList, updateOrAddRecord } from "@/api/health-punch";
import { useUserStoreHook } from "@/store/modules/user";

defineOptions({
  name: "AllPunchRecoed"
});

const recordListData = reactive({
  totle: 0,
  list: []
});

const userId = ref(useUserStoreHook()?.userId);
console.log("当前用户id=", userId.value);

onMounted(() => {
  console.log(`the component is now mounted.`);
  getRecordListApi(null);
});

// 接口请求参数
const findCondition = reactive({
  phoneNub: "",
  userName: "",
  address: ""
});
const pageSet = reactive({
  pageNub: 1,
  pageSize: 8
});

async function getRecordListApi(findCondition) {
  const result = await recordList(pageSet.pageNub, pageSet.pageSize, 0, findCondition);
  console.log("接口数据================", result);
  if (result.code == 200) {
    message("获取成功", { type: "success" });
    // 赋值
    recordListData.list = result.data.records;
    recordListData.totle = result.data.total;
  } else {
    message(result.message, { type: "error" });
  }
}

// 条件查询
function onSubmit() {
  getRecordListApi(findCondition);
}
function onReset() {
  findCondition.address = "";
  findCondition.phoneNub = "";
  findCondition.userName = "";
}

// 分页
function handleCurrentChange(pageNub) {
  console.log(pageNub);
  getRecordListApi(findCondition);
}

// ==========添加用户===========================
const addFormVisible = ref(false);

const addRecordCardData = reactive({
  userId: userId.value,
  bodyState: "",
  temperature: "",
  yinOrYang: "阴性",
  recordAddress: "萍乡学院12栋"
});

async function submitAddForm() {
  const result = await updateOrAddRecord(1, addRecordCardData);
  console.log(result);
  if (result.code == 200) {
    message("添加成功", { type: "success" });
    // 关弹窗 刷新数据
    getRecordListApi(findCondition);
    addFormVisible.value = false;
  } else {
    message(result.message, { type: "error" });
  }
}

// ==========编辑用户===========================
const editFormVisible = ref(false);

const updateRecordCardData = reactive({
  id: undefined,
  bodyState: "",
  temperature: "",
  yinOrYang: "",
  recordAddress: ""
});

function editUser(row) {
  console.log("编辑", row);
  editFormVisible.value = true;

  updateRecordCardData.id = row.id;
  updateRecordCardData.bodyState = row.bodyState;
  updateRecordCardData.temperature = row.temperature;
  updateRecordCardData.yinOrYang = row.yinOrYang;
  updateRecordCardData.recordAddress = row.recordAddress;
}

async function submitEditForm() {
  console.log("submit!", updateRecordCardData);
  const result = await updateOrAddRecord(2, updateRecordCardData);
  console.log(result);
  if (result.code == 200) {
    message("添加成功", { type: "success" });
    // 关弹窗 刷新数据
    getRecordListApi(findCondition);
    editFormVisible.value = false;
  } else {
    message(result.message, { type: "error" });
  }
}

// ====删除=========
async function deleteRecord(id: number) {
  console.log("删除=", id);
  const result = await deleteRecordById(id);
  console.log(result);
  if (result.code == 200) {
    message("删除成功", { type: "success" });
    // 关弹窗 刷新数据
    getRecordListApi(findCondition);
    editFormVisible.value = false;
  } else {
    message(result.message, { type: "error" });
  }
}
</script>
